/*
 *
 */

/*if (!tibco.admin.enterpriseAssets.shdResDefs.ShdResDefDetails) {
    tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/ShdResDefDetails.js","tibco.admin");
} */

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.HTTP.ShdHttpResDefs", //the full name of the class to create
        jsx3.lang.Object, //  extends class
        [tibco.admin.enterpriseAssets.shdResDefs.ShdResDefsInterface], // implements interface
        function (plugin) {

            plugin.prototype.details = null;
            // TODO: handle multiple instances?

            // Implementation of the ShdResDefsInterface
            plugin.instance = null

            plugin.prototype.initPlugin = function() {
                tibco.admin.Main.loadResource("tibco/admin/enterpriseAssets/shdResDefs/HTTP/jss/jss.xml", "tibco.admin", "jss_25B", "ljss", false);
				tibco.admin.Main.loadResource("tibco/admin/enterpriseAssets/subVars/jss/jss.xml", "tibco.admin", "jssSvar_25B", "ljss", false);
            }

            plugin.prototype.destroyPlugin = function() {
            }

            plugin.getInstance = function(args) {
                if(!this.instance) {
                    this.instance = new tibco.admin.enterpriseAssets.shdResDefs.HTTP.ShdHttpResDefs();
                }
                return this.instance;
            }

            plugin.prototype.loadComponent = function(parent, master, id, name, description) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.HTTP.ShdHttpResDefDetails(master, parent, id,
                        name, description, me);
                this.details.loadView();
                return this.details;
            }

            plugin.prototype.getType = function() {
                return "HTTP";
            }

            plugin.prototype.getAddText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@AddText_HTTP");
            }

            plugin.prototype.getImagePath = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@addin@HTTP Icon Path");
            }

            plugin.prototype.addSharedResourceDefinition = function(parent, master) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.HTTP.ShdHttpResDefDetails(master, parent,
                        null, null, null, me);
                this.details.loadAdd();
                return this.details;
            }
            // End implementation of the ShdResDefsInterface
        });

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.HTTP.ShdHttpResDefDetails", //the full name of the class
        tibco.admin.enterpriseAssets.shdResDefs.ShdResDefDetails, //  extends ShdResDefDetails
        null, // implements no interface
        function (details) {
            details.ONSSLYES = "tibco.admin.enterpriseAssets.shdResDefs.HTTP.SSLYES";
            details.ONSSLNO = "tibco.admin.enterpriseAssets.shdResDefs.HTTP.SSLNO";
            details.ONPICKSSL = "tibco.admin.enterpriseAssets.shdResDefs.HTTP.PICKSSL";

            details.prototype.helpID = "EnterpriseAssets.SharedResourceDefinitions.SharedResourceDefinitions.HTTPServer";

            details.prototype.init = function(master, parent, id, name, description, handler) {
                this.jsxsuper(master, parent, id, name, description, handler);
                this.editableComponentList.addAll(["txtHost_25B","txtBtnHost_25B","lblSvarstxtHost_25B", "txtPort_25B", "txtBtnPort_25B","lblSvarstxtPort_25B", "radDNSYes_25B", "radDNSNo_25B", "txtMaxPost_25B", "txtRedirPort_25B","lblSvarstxtRedirPort_25B", "txtBtnRedirPort_25B", "txtCnxTimeout_25B", "txtMinThreads_25B", "txtBtnMinThreads_25B", "lblSvarstxtMinThreads_25B", "txtMaxThreads_25B", "txtBtnMaxThreads_25B", "lblSvarstxtMaxThreads_25B", "radDsblUpTimeoutYes_25B", "radDsblUpTimeoutNo_25B", "radUseSSLYes_25B", "radUseSSLNo_25B", "btnSslConfigName_25B"], this.editableComponentList.size());
                this.rwComponentList.addAll(["txtHost_25B","txtBtnHost_25B","lblSvarstxtHost_25B", "txtPort_25B", "txtBtnPort_25B","lblSvarstxtPort_25B", "blkDNSYesNo_25B", "txtMaxPost_25B", "txtRedirPort_25B","lblSvarstxtRedirPort_25B", "txtCnxTimeout_25B", "txtBtnRedirPort_25B", "txtMinThreads_25B", "txtBtnMinThreads_25B", "lblSvarstxtMinThreads_25B", "txtMaxThreads_25B", "txtBtnMaxThreads_25B", "lblSvarstxtMaxThreads_25B", "blkDsblUpTimeoutYesNo_25B", "blkUseSSLYesNo_25B", "btnSslConfigName_25B"], this.rwComponentList.size());
                this.roComponentList.addAll(["lblcHost_25B", "lblcPort_25B", "lblcDNS_25B", "lblcMaxPost_25B", "lblcRedirPort_25B", "lblcCnxTimeout_25B", "lblcMinThreads_25B", "lblcMaxThreads_25B", "lblcDsblUpTimeout_25B", "lblcUseSSL_25B", "lblcSslConfigName_25B"], this.roComponentList.size());
            }

            details.prototype.getType = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@SRDType_HTTP");
            }

            details.prototype.getHelpId = function() {
                return this.helpID;
            }

            details.prototype.getDetailsOperation = function() {
                return "getHttpResourceDetails";
            }
            details.prototype.getDetailsOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/HTTP/rules/getHttpResourceDetails.xml";
            }
            details.prototype.editOperation = function() {
                return "editHttpResource";
            }
            details.prototype.editOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/HTTP/rules/editHttpResource.xml";
            }
            details.prototype.addOperation = function() {
                return "addHttpResource";
            }
            details.prototype.addOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/HTTP/rules/addHttpResource.xml";
            }
            details.prototype.getDetailsComponent = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/HTTP/components/shdHTTPResDefDetails.xml";
            }
            details.prototype.getSecondaryDetailsComponent = function() {
                return null;
            }

            // TODO: make name unique
            details.prototype.getDefaultName = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultName_HTTP");
            }
            details.prototype.getDefaultDescription = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultDescription_HTTP");
            }

            details.prototype.initializeSpecificDetails = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.mainComponent.getDescendantOfName("radUseSSLYes_25B").subscribe(details.ONSSLYES,
                            this, this.showSslDetails);
                    this.mainComponent.getDescendantOfName("radUseSSLNo_25B").subscribe(details.ONSSLNO,
                            this, this.hideSslDetails);
                    this.mainComponent.getDescendantOfName("btnSslConfigName_25B").subscribe(details.ONPICKSSL,
                            this, this.pickSSL);
                }
                this.jsxsuper(objEvent);

                this.initializeHTTPParam(objEvent);
				objEvent.obj.handler = this;//070818
            }

            details.prototype.initializeSpecificAdd = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.mainComponent.getDescendantOfName("radUseSSLYes_25B").subscribe(details.ONSSLYES,
                            this, this.showSslDetails);
                    this.mainComponent.getDescendantOfName("radUseSSLNo_25B").subscribe(details.ONSSLNO,
                            this, this.hideSslDetails);
                    this.mainComponent.getDescendantOfName("btnSslConfigName_25B").subscribe(details.ONPICKSSL,
                            this, this.pickSSL);
                }
                this.jsxsuper(objEvent);
				objEvent.obj.handler = this;//070818
                this.initializeHTTPParam(objEvent);
            }

            details.prototype.ongetConfigDetailsSuccess = function(objEvent) {
                var response = tibco.admin.Main.getServer().Cache.getDocument("xmlCacheIDGetHttpDetailsResponse_25B").getRecordNode("jsxroot");
                this.jsxsuper(response);

                var server = tibco.admin.Main.getServer(); 
                var value = response.getAttribute("host");
                this.mainComponent.getDescendantOfName("lblcHost_25B").setText(value, true);
                this.mainComponent.getDescendantOfName("txtHost_25B").setValue(value, true);
                value = response.getAttribute("port");
                this.mainComponent.getDescendantOfName("lblcPort_25B").setText(value, true);
                this.mainComponent.getDescendantOfName("txtPort_25B").setValue(value, true);
                value = response.getAttribute("enableDNSLookup");
                this.mainComponent.getDescendantOfName("lblcDNS_25B").setText((value == "true") ? server.getDynamicProperty("@addin@radBtn Yes") : server.getDynamicProperty("@addin@radBtn No"), true);
                this.mainComponent.getDescendantOfName("radDNSYes_25B").setGroupValue(value, true);
                value = response.getAttribute("maxPostSize");
                this.mainComponent.getDescendantOfName("lblcMaxPost_25B").setText(value, true);
                this.mainComponent.getDescendantOfName("txtMaxPost_25B").setValue(value, true);
                value = response.getAttribute("redirectPort");
                this.mainComponent.getDescendantOfName("lblcRedirPort_25B").setText(value, true);
                this.mainComponent.getDescendantOfName("txtRedirPort_25B").setValue(value, true);
                value = response.getAttribute("connectionTimeout");
                this.mainComponent.getDescendantOfName("lblcCnxTimeout_25B").setText(value, true);
                this.mainComponent.getDescendantOfName("txtCnxTimeout_25B").setValue(value, true);
                value = response.getAttribute("minThreads");
                this.mainComponent.getDescendantOfName("lblcMinThreads_25B").setText(value, true);
                this.mainComponent.getDescendantOfName("txtMinThreads_25B").setValue(value, true);
                value = response.getAttribute("maxThreads");
                this.mainComponent.getDescendantOfName("lblcMaxThreads_25B").setText(value, true);
                this.mainComponent.getDescendantOfName("txtMaxThreads_25B").setValue(value, true);
                value = response.getAttribute("disableUploadTime");
                this.mainComponent.getDescendantOfName("lblcDsblUpTimeout_25B").setText((value == "true") ? server.getDynamicProperty("@addin@radBtn Yes") : server.getDynamicProperty("@addin@radBtn Yes"), true);
                this.mainComponent.getDescendantOfName("radDsblUpTimeoutYes_25B").setGroupValue(value, true);
                value = response.getAttribute("SSLResourceName");
                this.mainComponent.getDescendantOfName("lblcUseSSL_25B").setText((value == "true") ? server.getDynamicProperty("@addin@radBtn Yes") : server.getDynamicProperty("@addin@radBtn No"), true);
                this.mainComponent.getDescendantOfName("radUseSSLYes_25B").setGroupValue(value ? "true" : "false", true);
                if(value) {
                    this.mainComponent.getDescendantOfName("lblcSslConfigName_25B").setText(value, true);
                    this.mainComponent.getDescendantOfName("btnSslConfigName_25B").setDynamicProperty("jsxtext", null);
                    this.mainComponent.getDescendantOfName("btnSslConfigName_25B").setText(value, true);
                    this.showSslDetails();
                }
                else {
                    this.hideSslDetails();
                }

				var responseSvars = tibco.admin.Main.getServer().Cache.getDocument("xmlCacheListOfSubstitutionBinding").getRecordNode("jsxroot");
				var sVarsgetArray = responseSvars.getChildNodes().toArray();
				for(var i = 0 ; i < sVarsgetArray.length ; i++){
					var svarFieldName = sVarsgetArray[i].getAttribute("fieldName");
					var svarTemplate = sVarsgetArray[i].getAttribute("template");
					var txtRelateId = null;

					if(svarFieldName == "host"){
						txtRelateId = "Host_25B";
					}else if(svarFieldName == "port"){
						txtRelateId = "Port_25B";
					}else if(svarFieldName == "redirectPort"){
						txtRelateId = "RedirPort_25B";
					}else if(svarFieldName == "minThreads"){
						txtRelateId = "MinThreads_25B";
					}else if(svarFieldName == "maxThreads"){
						txtRelateId = "MaxThreads_25B";
					}
					if(txtRelateId != "" && txtRelateId != null && txtRelateId != undefined){
						this.mainComponent.getDescendantOfName("lblc" + txtRelateId).setText(svarTemplate + ":" + response.getAttribute(svarFieldName),true);
						server.getJSXByName("txt" + txtRelateId).setValue(svarTemplate + ":" + response.getAttribute(svarFieldName));
						server.getJSXByName("txt" + txtRelateId).setColor("#9898A5",true);
						server.getJSXByName("txt" + txtRelateId).setBackgroundColor("#FFFEFF",true);
						//server.getJSXByName("lblSvars" + txtRelateId).setVisibility(jsx3.gui.Block.VISIBILITYVISIBLE, true);
						server.getJSXByName("txt" + txtRelateId).sVarName = svarTemplate;
						server.getJSXByName("txt" + txtRelateId).sVarValue = response.getAttribute(svarFieldName);
						server.getJSXByName("txt" + txtRelateId).isSvars = true;
					}
				}

                this.setEditable(true);
                this.initializeHTTPParam(objEvent);
            }

            details.prototype.showSslDetails = function(event) {
                this.mainComponent.getDescendantOfName("blkSslConfigName_25B").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
            }

            details.prototype.hideSslDetails = function(event) {
                this.mainComponent.getDescendantOfName("blkSslConfigName_25B").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
            }

            details.prototype.pickSSL = function(event) {
                var pick = this.mainComponent.getDescendantOfName("btnSslConfigName_25B").getText();
                if(pick == tibco.admin.Main.getServer().getDynamicProperty("@ChooseSSLConfig_25B")) {
                    pick = null;
                }
                tibco.admin.enterpriseAssets.shdResDefs.ShdResDefPicker.pickChoice(event.source, tibco.admin.Main.getServer().getDynamicProperty("@SRDType_SSLServer"), pick);
            }

            details.prototype.setEditRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.setAddRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.setRequestDetails = function(objService) {
                objService.host = this.mainComponent.getDescendantOfName("txtHost_25B").isSvars == true ? this.mainComponent.getDescendantOfName("txtHost_25B").sVarValue :this.mainComponent.getDescendantOfName("txtHost_25B").getValue();
                objService.port = this.mainComponent.getDescendantOfName("txtPort_25B").isSvars == true ? this.mainComponent.getDescendantOfName("txtPort_25B").sVarValue :this.mainComponent.getDescendantOfName("txtPort_25B").getValue();
				objService.enableDNSLookup = this.mainComponent.getDescendantOfName("radDNSYes_25B").getGroupValue();
                objService.maxPostSize = this.mainComponent.getDescendantOfName("txtMaxPost_25B").getValue();
                objService.redirectPort = this.mainComponent.getDescendantOfName("txtRedirPort_25B").isSvars == true ? this.mainComponent.getDescendantOfName("txtRedirPort_25B").sVarValue :this.mainComponent.getDescendantOfName("txtRedirPort_25B").getValue();
                objService.connectionTimeout = this.mainComponent.getDescendantOfName("txtCnxTimeout_25B").getValue();
                objService.minThreads = this.mainComponent.getDescendantOfName("txtMinThreads_25B").isSvars == true ? this.mainComponent.getDescendantOfName("txtMinThreads_25B").sVarValue :this.mainComponent.getDescendantOfName("txtMinThreads_25B").getValue();
                objService.maxThreads = this.mainComponent.getDescendantOfName("txtMaxThreads_25B").isSvars == true ? this.mainComponent.getDescendantOfName("txtMaxThreads_25B").sVarValue :this.mainComponent.getDescendantOfName("txtMaxThreads_25B").getValue();
                objService.disableUploadTime = this.mainComponent.getDescendantOfName("radDsblUpTimeoutYes_25B").getGroupValue();
                if(this.mainComponent.getDescendantOfName("radUseSSLYes_25B").getGroupValue() == "true") {
                    objService.SSLResourceName = this.mainComponent.getDescendantOfName("btnSslConfigName_25B").getText();
                    objService.ssl = "true"
                }
                else {
                    objService.SSLResourceName = null
                    objService.ssl = "false"
                }

				var objCDFDoc = jsx3.xml.CDF.newDocument();

				var isSvarsArray = ["txtHost_25B","txtPort_25B","txtRedirPort_25B","txtMinThreads_25B","txtMaxThreads_25B"];
				var isSvarsNameArray = ["host","port","redirectPort","minThreads","maxThreads"];
				for(var i = 0 ; i < isSvarsArray.length ; i++){
					var sVarsTxtObj = this.mainComponent.getDescendantOfName(isSvarsArray[i]);
					if(sVarsTxtObj.isSvars == true){
						var record = objCDFDoc.createNode(jsx3.xml.Entity.TYPEELEMENT, "record");
						record.setAttribute("fieldName", isSvarsNameArray[i]);
						record.setAttribute("template", sVarsTxtObj.sVarName);
						objCDFDoc.appendChild(record);
					}
				}
				tibco.admin.Main.getServer().getCache().setDocument("setHTTPSvarsCacheID_25", objCDFDoc);
            }


            /*070818
               * if user doesn't fishish all the required fields, the "save" button will be disabled.  part 2 for http
               */
            details.prototype.valiPort = function(obj, strValue) {
                /* not needed, coz ShdHttpResDefDetails extends ShdResDefDetails
                    var srd = obj.getAncestorOfName("lytDetailsMain_25").handler;
                    */

                this.passedItems["portPassed"] = tibco.admin.util.Validation.doValidatePort(obj, strValue);
                this.enableSaveBtn4Add();
            }

            details.prototype.valiHost = function(obj, strValue) {
                this.passedItems["hostPassed"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@Host_25B"));
                this.enableSaveBtn4Add();
            }

            details.prototype.initializeHTTPParam = function(objEvent) {
                this.passedItems["portPassed"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtPort_25B").getValue())?true:false;
                this.passedItems["hostPassed"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtHost_25B").getValue())?true:false;
                this.enableSaveBtn4Add();
            }

			details.prototype.typeValue = null;
			details.prototype.relateTxtId = null;

            details.prototype.callSvOnclick = function(relateTxtId,type) {
                this.dialog = tibco.admin.Main.loadUIResource(tibco.admin.Main.getServer().getBodyBlock(), "tibco.admin", "tibco/admin/enterpriseAssets/shdResDefs/HTTP/components/shdHTTPSetSvars.xml", true);
				this.typeValue = type;
				this.relateTxtId = relateTxtId;
                var server = tibco.admin.Main.getServer();
				var titleValue = null;
				if(type == "2"){
					titleValue = server.getDynamicProperty("@@SelTypeInt_56L");
				}else if(type == "1"){
					titleValue = server.getDynamicProperty("@@SelTypeString_56L");
				}
                this.dialog.getFirstChildOfType("jsx3.gui.WindowBar").setText(server.getDynamicProperty("@wbarSvars_Text", titleValue),true);
				this.dialog.parent = this;

				var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/shdResDefs/HTTP/rules/getHTTPSvarsInEnterprise.xml", "getSvarsInEnterprise", "Get Substitution Variables");
				objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSubstitutionVarsEndpointURL());
				objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onCallGetHTTPSvarsInEnterpriseSuccess); 
				objService.doCall();
            }

			details.prototype.onCallGetHTTPSvarsInEnterpriseSuccess = function(objEvent){
				this.dialog.getDescendantOfName("mtxSelectSvars_25B").repaint();
				var rowArrayIds = this.dialog.getDescendantOfName("mtxSelectSvars_25B").getSortedIds();
				for(var i = 0 ; i < rowArrayIds.length ; i++){
					var objRec = this.dialog.getDescendantOfName("mtxSelectSvars_25B").getRecordNode(rowArrayIds[i]);
					if(objRec.getAttribute("type") == this.typeValue){
						this.dialog.getDescendantOfName("mtxSelectSvars_25B").deleteRecord(rowArrayIds[i],true);
					}
				}
				rowArrayIds = this.dialog.getDescendantOfName("mtxSelectSvars_25B").getSortedIds();
				if(rowArrayIds.length == 0){
					this.dialog.getDescendantOfName("blkNullLabel_25B").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
				}else{
					this.dialog.getDescendantOfName("blkNullLabel_25B").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
				}
			}

			details.prototype.onMatrixSelected = function(blkObj,objEVENT,strRECORDID,objCOLUMN){
				var server = tibco.admin.Main.getServer();
				var objCDFDocument = server.getCache().getDocument(server.getJSXByName("mtxSelectSvars_25B").getXMLId());
				var objRec = objCDFDocument.getRecordNode(strRECORDID);
				var sVarName = null;
				var sVarValue = null;
				if(objRec) {
					sVarName = objRec.getAttribute("name");
					sVarValue = objRec.getAttribute("value");
					server.getJSXByName(this.relateTxtId).setValue(sVarName + ":" + sVarValue);
					server.getJSXByName(this.relateTxtId).setColor("#9898A5",true);
					server.getJSXByName(this.relateTxtId).setBackgroundColor("#FFFEFF",true);
					server.getJSXByName("lblSvars" + this.relateTxtId).setVisibility(jsx3.gui.Block.VISIBILITYVISIBLE, true);
					server.getJSXByName(this.relateTxtId).sVarName = sVarName;
					server.getJSXByName(this.relateTxtId).sVarValue = sVarValue;
					server.getJSXByName(this.relateTxtId).isSvars = true;
				}
				blkObj.getAncestorOfType(jsx3.gui.Dialog).doClose();
			}

			details.prototype.onKeyDown = function(blkObj,objEVENT){
				if(blkObj.isSvars == true){
					blkObj.sVarName = "";
					blkObj.isSvars = false;
					blkObj.setValue("");
					blkObj.setColor("#000000",true);
					tibco.admin.Main.getServer().getJSXByName("lblSvars" + blkObj.jsxname).setVisibility(jsx3.gui.Block.VISIBILITYHIDDEN, true);
				}
			}

        });
